package com.xq.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xq.shop.config.Redis;
import com.xq.shop.dao.domain.entity.ExchangeRate;
import com.xq.shop.dao.mapper.ExchangeRateMapper;
import com.xq.shop.dto.EditExchangeRateDto;
import com.xq.shop.dto.QueryDto;
import com.xq.shop.job.ScheduleUtils;
import com.xq.shop.service.IExchangeRateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xq.shop.vo.ExchangeRateVo;
import enums.DataStatus;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import utils.BaseConstants;
import utils.BasePage;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 汇率表 服务实现类
 * </p>
 *
 * @author gaoxiaojin
 * @since 2024-07-16
 */
@Service
public class ExchangeRateServiceImpl extends ServiceImpl<ExchangeRateMapper, ExchangeRate> implements IExchangeRateService {

    @Resource
    private Redis redis;

    @Resource
    public ScheduleUtils utils;

    @Override
    public BasePage<ExchangeRateVo> pageQuery(QueryDto dto) {
        LambdaQueryWrapper<ExchangeRate> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ExchangeRate::getStatus, DataStatus.NORMAL.getCode());
        if (Objects.nonNull(dto.getStartTime()) && Objects.nonNull(dto.getEndTime())) {
            wrapper.between(ExchangeRate::getCreateTime, dto.getStartTime(), dto.getEndTime());
        }
        wrapper.orderByDesc(ExchangeRate::getId);
        IPage<ExchangeRate> page = page(new Page<>(dto.getPage(), dto.getLimit()), wrapper);
        List<ExchangeRate> exchangeRateList = page.getRecords();
        List<ExchangeRateVo> voList = beanToVo(exchangeRateList);
        return new BasePage<ExchangeRateVo>().initPage(dto.getPage(), dto.getLimit(), page.getTotal(), page.getPages(), voList);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(EditExchangeRateDto dto) {
        ExchangeRate exchangeRate = getById(dto.getId());
        if (Objects.nonNull(exchangeRate)) {
            if (Objects.isNull(exchangeRate.getOldExchangeRate())) {
                exchangeRate.setOldExchangeRate(exchangeRate.getExchangeRate());
            }
            exchangeRate.setExchangeRate(dto.getExchangeRate());
            updateById(exchangeRate);
           // utils.cancelScheduledTask("exchangeRate");
        }
        redis.remove("exchangeRate");
    }

    private List<ExchangeRateVo> beanToVo(List<ExchangeRate> exchangeRateList) {
        List<ExchangeRateVo> voList = new ArrayList<>();
        exchangeRateList.forEach(exchangeRate -> {
            ExchangeRateVo vo = new ExchangeRateVo();
            BeanUtils.copyProperties(exchangeRate, vo);
            vo.setCreateTime(BaseConstants.SDF1.format(exchangeRate.getCreateTime()));
            voList.add(vo);
        });
        return voList;
    }
}
